import type { BaiduElements } from '../../../../../../../../types/constants'
import { ThemeVarService } from '../../../../../../../../lib/abstract/style/theme_var/service'
import { PageHandler } from '../../../../../../../../lib/page_handler'
import { BaiduPageType } from '../../../../../../../../lib/page_handler/platform/baidu/types'
import {
  PAGE_ELEMENTS,
  SUPPORTED_PLATFORMS,
} from '../../../../../../../../types/constants'
import { SearchCommonStyles } from '../../common'

const BASE_STYLES = `
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  margin: 0 auto !important;
`

export class BaiduCommonPage {
  elements: BaiduElements = PAGE_ELEMENTS[SUPPORTED_PLATFORMS.BAI_DU]
  private static instance: BaiduCommonPage
  private readonly pageHandler: PageHandler = PageHandler.getInstance()
  private readonly themeVarService: ThemeVarService = ThemeVarService.getInstance()
  protected readonly searchCommonStyles: SearchCommonStyles = SearchCommonStyles.getInstance()
  private pageType: BaiduPageType | 'default' = this.pageHandler.pages.baidu.pageType

  /**
   * 获取单例实例
   */
  public static getInstance(): BaiduCommonPage {
    if (!BaiduCommonPage.instance) {
      BaiduCommonPage.instance = new BaiduCommonPage()
    }
    return BaiduCommonPage.instance
  }

  init(): void {
  }

  ItemStyles(): string {
    return `
&:hover {
  background-color: #2d4646 !important;
  color: #ffffff !important;
}
`
  }

  /**
   * 停止按钮图标样式
   */
  stopButtonIconStyles(): string {
    return `
/* 停止按钮 */
svg {
  circle {
    stroke: var(${this.themeVarService.search.default.text}) !important;
  }
  rect {
    fill: var(${this.themeVarService.search.default.text}) !important;
  }
}
`
  }

  /**
   * 搜索框样式
   */
  searchBoxStyles(): string {
    const notSupportPages = [
      BaiduPageType.Index,
    ]

    return notSupportPages.includes(this.pageType as BaiduPageType)
      ? ''
      : `
  /* 搜索框 */
  #head {
    background: var(${this.themeVarService.search.header.background}) !important;
    
    .s_form {
      ${BASE_STYLES}
      padding: 0 !important;
    }
    
    #u {
      display: flex !important;
          
      > a:not(.lb) {
        color: var(${this.themeVarService.search.default.text}) !important;
        
        &:hover {
          color: var(${this.themeVarService.link.default.text}) !important;
        }
      }
      
      > a.lb {
        &:hover {
          background-color: #315efb !important;
        }
      }
    }
  }
`
  }

  /**
   * 折叠开关样式
   */
  foldSwitchStyles(): string {
    return `
/* 展开文本/展开图标 */
.cos-fold-switch-text, .cos-fold-switch-icon {
  color: var(${this.themeVarService.answer.switch.mask.text}) !important;
}
`
  }

  asideStyles(): string {
    const supportPages = [
      BaiduPageType.Index,
      BaiduPageType.Web,
      BaiduPageType.News,
    ]

    return !supportPages.includes(this.pageType as BaiduPageType)
      ? ''
      : `
  /* 侧边栏 */
  #aside-wrapper {
    #chat-aside-main {
      background: var(${this.themeVarService.search.header.background}) !important;
      color: var(${this.themeVarService.search.default.text}) !important;
    }
  }
`
  }

  filterStyles(): string {
    const supportPages = [
      BaiduPageType.Web,
    ]

    return !supportPages.includes(this.pageType as BaiduPageType)
      ? ''
      : `
  /* 筛选 */
  #tsn_inner {
    span {
      color: var(${this.themeVarService.search.default.text}) !important;
    }
  }
`
  }

  goToPageStyles(): string {
    const supportPages = [
      BaiduPageType.News,
    ]

    return !supportPages.includes(this.pageType as BaiduPageType)
      ? ''
      : `
  /* 跳转页面 */
  #gotoPage {
    margin-left: 15px !important;
    
    a {
      color: var(${this.themeVarService.link.default.text}) !important;
    }
  }
`
  }

  hideStyles(): string {
    const supportPages = [
      BaiduPageType.Web,
      BaiduPageType.News,
    ]

    return !supportPages.includes(this.pageType as BaiduPageType)
      ? ''
      : `
  /* 隐藏右侧内容 */
  #content_right {
    display: none !important;
  }  

  /* 隐藏广告 */
  #content_left {
    div[tpl="se_com_default"]:has([data-tuiguang]), div[tpl="se_com_default"]:has(.se_st_footer a) {
      display: none !important;
    }
  }
`
  }

  tabStyles(): string {
    const supportPages = [
      BaiduPageType.Web,
      BaiduPageType.News,
      BaiduPageType.Video,
      BaiduPageType.Note,
    ]

    return !supportPages.includes(this.pageType as BaiduPageType)
      ? ''
      : `
  /* 搜索分类 */   
  #s_tab {
    ${BASE_STYLES}
    background: var(${this.themeVarService.search.header.background}) !important;
    color: var(${this.themeVarService.search.default.text}) !important;
    width: 100% !important;
    max-width: 100% !important;
    ${this.pageType === BaiduPageType.Note
      ? `
        padding-left: 0 !important;
        padding-right: 0 !important;
      `
      : ''}
    
    #s_tab_inner, .s_tab_inner {
      align-items: baseline !important;
      ${this.pageType !== BaiduPageType.Web
        ? `
          width: fit-content !important;
          padding-left: 0 !important;
          `
        : ''}
      gap: ${this.themeVarService.search.resultItem.gap} !important;
      justify-content: center !important;
      
      .cur-tab, .s-tab-item {
        line-height: inherit !important;
      }

      .cur-tab {
        color: var(${this.themeVarService.tab.textActive}) !important;
        
        &::after {
          background-color: var(${this.themeVarService.tab.textActive}) !important;
          width: auto !important;
          margin: 0 !important;
          height: 2px !important;
        }
        
        &::before {
          color: var(${this.themeVarService.tab.textActive}) !important;
        }
      }
      
      .s-tab-item {
        color: var(${this.themeVarService.tab.text}) !important;

        &::before {
          color: var(${this.themeVarService.tab.text}) !important;
        }

        &:hover {
          color: var(${this.themeVarService.tab.textHover}) !important;
          
          &::before {
            color: var(${this.themeVarService.tab.textHover}) !important;
          }
        }
      }
    }
  }
`
  }

  getMainStyles(): string {
    const supportPages = [
      BaiduPageType.Web,
      BaiduPageType.News,
      BaiduPageType.Video,
      BaiduPageType.Note,
    ]

    return !supportPages.includes(this.pageType as BaiduPageType)
      ? ''
      : `
  ${this.searchBoxStyles()}
  ${this.tabStyles()}
  ${this.asideStyles()}
  ${this.hideStyles()}
  ${this.filterStyles()}
  ${this.goToPageStyles()}
`
  }
}
